﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Show_MonthQoQ : UserPage
{
    int CellNum = 0;
    int LastUnLink = 0;
    int Unlink = 0;
    int LastDura = 0;
    int Dura = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AttributeBind();
            Report_Bind();
        }        
    }

    protected void Query_Click(object sender, EventArgs e)
    {
        Report_Bind();
    }

    protected void Excel_Click(object sender, EventArgs e)
    {
        Tools.DGToExcel(Report, DurationList);
    }

    private void AttributeBind()
    {
        Month_A.Text = DateTime.Now.ToString("yyyy年MM月");
        Month_A.Attributes.Add("onclick", "WdatePicker({el:$dp.$('EndDate'),dateFmt:'yyyy年MM月',maxDate:'%y年%M月',minDate:'2014年3月'});");
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

    private void Report_Bind()
    {
        string sqlstr = "AB_GetReport_Pk.GetUNLinkBy";
        OracleParameter[] _params = {
                new OracleParameter("v_Month",OracleType.VarChar,6),
				new OracleParameter("V_Class",OracleType.VarChar,4),	
                new OracleParameter("v_County",OracleType.VarChar,6),	
				new OracleParameter("resultSet",OracleType.Cursor)
            };
        _params[0].Value = Month_A.Text.Replace("年", "").Replace("月","");
        _params[1].Value = CellClass_A.SelectedValue;
        _params[2].Value = "";
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            _params[2].Value = dr["company"].ToString();
        }
        _params[3].Direction = ParameterDirection.Output;
        DataSet ds = OracleHelper.ExecuteDataSet(sqlstr, CommandType.StoredProcedure, _params);
        Report.DataSource = ds;
        Report.DataBind();
        DurationList.DataSource = ds;
        DurationList.DataBind();
    }

    protected void Report_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection tcHeader = e.Row.Cells;
            tcHeader.Clear();

            //第一行表头
            tcHeader.Add(new TableHeaderCell());
            tcHeader[0].Attributes.Add("rowspan", "2"); 
            tcHeader[0].Text = "县分";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[1].Attributes.Add("rowspan", "2");
            tcHeader[1].Text = "基站数量";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[2].Attributes.Add("colspan", "3");
            tcHeader[2].Text = "基站断站次数";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[3].Attributes.Add("colspan", "4");
            tcHeader[3].Text = "基站断站率</th></tr><tr class='ui-widget-header'>";

            //第二行
            tcHeader.Add(new TableHeaderCell());
            tcHeader[4].Text = "上月";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[5].Text = "当月";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[6].Text = "变化";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[7].Text = "上月";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[8].Text = "当月";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[9].Text = "变化";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[10].Text = "环比";
        }
    }

    protected void DurationList_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection tcHeader = e.Row.Cells;
            tcHeader.Clear();

            //第一行表头
            tcHeader.Add(new TableHeaderCell());
            tcHeader[0].Attributes.Add("rowspan", "2"); 
            tcHeader[0].Text = "县分";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[1].Attributes.Add("rowspan", "2");
            tcHeader[1].Text = "基站数量";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[2].Attributes.Add("colspan", "3");
            tcHeader[2].Text = "基站断站时长";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[3].Attributes.Add("colspan", "4");
            tcHeader[3].Text = "基站平均断站时长</th></tr><tr class='ui-widget-header'>";

            //第二行
            tcHeader.Add(new TableHeaderCell());
            tcHeader[4].Text = "上月";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[5].Text = "当月";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[6].Text = "变化";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[7].Text = "上月";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[8].Text = "当月";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[9].Text = "变化";
            tcHeader.Add(new TableHeaderCell());
            tcHeader[10].Text = "环比";
        }
    }
    protected void Report_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView row = (DataRowView)e.Row.DataItem;
            CellNum += Convert.ToInt32(row[1].ToString());
            LastUnLink += Convert.ToInt32(row[2].ToString());
            Unlink += Convert.ToInt32(row[3].ToString());
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D5E5EA'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "合计";
            e.Row.Cells[1].Text = CellNum.ToString();
            e.Row.Cells[2].Text = LastUnLink.ToString();
            e.Row.Cells[3].Text = Unlink.ToString();
            e.Row.Cells[4].Text = (Unlink - LastUnLink).ToString();
            e.Row.Cells[5].Text = String.Format("{0:0.0000}", (double)LastUnLink / CellNum);
            e.Row.Cells[6].Text = String.Format("{0:0.0000}", (double)Unlink / CellNum);
            e.Row.Cells[7].Text = String.Format("{0:0.0000}", (double)Unlink / CellNum - (double)LastUnLink / CellNum);
            e.Row.Cells[8].Text = String.Format("{0:#.##%}"
                , ((double)Unlink / CellNum - (double)LastUnLink / CellNum) / ((double)LastUnLink / CellNum));
            e.Row.CssClass = "cssFooter";
        }
    }
    protected void DurationList_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView row = (DataRowView)e.Row.DataItem;              
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D5E5EA'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
            LastDura += StringToDura(row[5].ToString());
            Dura += StringToDura(row[6].ToString());
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "合计";
            e.Row.Cells[1].Text = CellNum.ToString();
            e.Row.Cells[2].Text = IntToDura(LastDura);
            e.Row.Cells[3].Text = IntToDura(Dura);
            e.Row.Cells[4].Text = IntToDura(Dura - LastDura);
            e.Row.Cells[5].Text = IntToDura(LastDura / CellNum);
            e.Row.Cells[6].Text = IntToDura(Dura / CellNum);
            e.Row.Cells[7].Text = IntToDura(Dura / CellNum - LastDura / CellNum);
            e.Row.Cells[8].Text = String.Format("{0:#.##%}"
                , (double)(Dura / CellNum - LastDura / CellNum) / (double)(LastDura / CellNum));
            e.Row.CssClass = "cssFooter";
        }
    }

    private int StringToDura(string argDura)
    {
        int d = 0;
        if (argDura.Equals("0"))
        {
            return d;
        }
        else
        {
            string[] ds = argDura.Split(':');
            d = Int32.Parse(ds[0]) * 60 * 60 + Int32.Parse(ds[1]) * 60 + Int32.Parse(ds[2]);
        }
        return d;
    }

    private String IntToDura(int argDura)
    {
        string  s = "0";
        if (argDura == 0)
        {
            return s;
        }
        else if (argDura > 0)
        {
            s = (argDura / 3600).ToString() + ":" + (argDura - argDura / 3600 * 3600) / 60 + ":" + argDura % 60;
        }
        else
        {
            s = (argDura / 3600).ToString() + ":" + (argDura - argDura / 3600 * 3600) / 60 + ":" + argDura % 60;
            s = "-" + s.Replace("-","");
        }
        return s;
    }
}
